﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace biaoqian
{
    public partial class Form4 : Form
    {
        LDhuoyunEntities db = new LDhuoyunEntities();
        Class1 one = new Class1();
        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            cxsj();
            GET_shxx();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            string zz = button1.Text;
            shclxx(zz);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string zz = button2.Text;
            shclxx(zz);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void shclxx(string zz)
        {
            int cx_id =int.Parse(label1.Text) ;
            int ss_id = int.Parse(label2.Text);
            int index = 0;
            int temp = 0;
            if (zz == "通过")
            {
                var list = db.YFJG.Where(x => x.jw_cx == cx_id).ToList();
                foreach(YFJG y in list)
                {
                    db.YFJG.Remove(y);
                }
                db.SaveChanges();

                var list2 = db.YFSH.Where(x => x.ss_cx == cx_id && x.ss_sh == "审核").ToList();
                foreach(YFSH y in list2)
                {
                    YFJG yf = new YFJG()
                    {
                        jw_bian=y.ss_bian,
                        jw_jt=y.ss_jt,
                        jw_jg = y.ss_jg,
                        jw_cx= cx_id,
                        jw_gs= ss_id,
                    };
                    db.YFJG.Add(yf);
                    YFSH yfsh = db.YFSH.Where(x => x.ss_id == y.ss_id).FirstOrDefault();
                    yfsh.ss_sh = "已审核";
                    index+= db.SaveChanges();
                }
                if (index > 0)
                {
                    CXSJ cx = db.CXSJ.Where(x => x.cx_id == cx_id).FirstOrDefault();
                    cx.cx_sh ="通过";
                    db.SaveChanges();
                    var cxlb = db.CXSJ.Where(x => x.cx_ss == ss_id).ToList();
                    foreach(CXSJ cxsjj in cxlb)
                    {
                        if (cxsjj.cx_sh != "通过")
                        {
                            temp++;
                        }
                    }
                    if (temp == 0)
                    {
                        SHZT zt = db.SHZT.Where(x => x.sh_zhuantiao == ss_id.ToString()).FirstOrDefault();
                        zt.sh_zt = "通过";
                        db.SaveChanges();
                    }
                    
                    MessageBox.Show(cx.cx_name + "通过价格审核");
                }
            }
            else
            {
                CXSJ cx = db.CXSJ.Where(x => x.cx_id == cx_id).FirstOrDefault();
                cx.cx_sh = "通过";
                db.SaveChanges();
                var cxlb = db.CXSJ.Where(x => x.cx_ss == ss_id).ToList();
                foreach (CXSJ cxsjj in cxlb)
                {
                    if (cxsjj.cx_sh != "通过")
                    {
                        temp++;
                    }
                }
                if (temp == 0)
                {
                    SHZT zt = db.SHZT.Where(x => x.sh_zhuantiao == ss_id.ToString()).FirstOrDefault();
                    zt.sh_zt = "通过";
                    db.SaveChanges();
                }

                MessageBox.Show(cx.cx_name + "未通过价格审核");
            }
        }
        private void GET_shxx()
        {
            var sHZT = db.SHZT.Where(x => x.sh_zt != "通过").ToList();
            DataTable dataTable = one.SHZT();
            foreach (SHZT shzt in sHZT)
            {
                int dks = int.Parse(shzt.sh_zhuantiao);
                string name = db.CYDW.Where(x => x.cy_id == dks).FirstOrDefault().cy_name;
                int cxdsh = db.CXSJ.Where(x => x.cx_ss == dks && x.cx_sh != "通过").Count();
                dataTable.Rows.Add(name, shzt.sh_lx, cxdsh,dks);

            };
            dataGridView2.DataSource = dataTable;
        }
        private void GET_dshcx(int ss_id)
        {
            var list = db.CXSJ.Where(x => x.cx_ss == ss_id && x.cx_sh == "待审核").ToList();
            DataTable dataTable = one.CXXX();
            foreach (CXSJ cx in list)
            {
                dataTable.Rows.Add(cx.cx_name, cx.CYDW.cy_name, cx.cx_sh, cx.cx_id);
            }
            dataGridView3.DataSource = dataTable;
        }
        private void GET_shsh(int thirdColumnCellValue)
        {
            var sh = db.YFSH.Where(x => x.ss_cx == thirdColumnCellValue && x.ss_sh == "审核").ToList();
            DataTable dataTable = one.JTJG();
            foreach(YFSH y in sh)
            {
                dataTable.Rows.Add(y.ss_bian, y.ss_jt, y.ss_jg);
            }
            dataGridView1.DataSource = dataTable;
        }
        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView3.Columns[e.ColumnIndex].Name == "View_details2")
            {
                int thirdColumnCellValue = int.Parse(dataGridView3.Rows[e.RowIndex].Cells[4].Value.ToString());

                GET_shsh(thirdColumnCellValue);
                label1.Text = thirdColumnCellValue.ToString();
            }
        }
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView2.Columns[e.ColumnIndex].Name == "View_details")
            {
                int thirdColumnCellValue = int.Parse(dataGridView2.Rows[e.RowIndex].Cells[4].Value.ToString());
                GET_dshcx(thirdColumnCellValue);
                label2.Text = thirdColumnCellValue.ToString();
            }
        }

        private void cxsj()
        {
            DataGridViewImageColumn deleteColumn = new DataGridViewImageColumn();
            deleteColumn.HeaderText = "查看详细";
            deleteColumn.Name = "View_details";
            deleteColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
            dataGridView2.Columns.Add(deleteColumn);

            DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
            name.HeaderText = "承运单位";
            name.DataPropertyName = "name";
            dataGridView2.Columns.Add(name);


            DataGridViewTextBoxColumn leix = new DataGridViewTextBoxColumn();
            leix.HeaderText = "审核类型";
            leix.DataPropertyName = "leix";
            dataGridView2.Columns.Add(leix);

            DataGridViewTextBoxColumn sun = new DataGridViewTextBoxColumn();
            sun.HeaderText = "待审核车型数量";
            sun.DataPropertyName = "sun";
            dataGridView2.Columns.Add(sun);

            DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
            id.Visible = false;
            id.HeaderText = "";
            id.DataPropertyName = "id";
            dataGridView2.Columns.Add(id);




            DataGridViewImageColumn deleteColumn2 = new DataGridViewImageColumn();
            deleteColumn2.HeaderText = "查看详细";
            deleteColumn2.Name = "View_details2";
            //deleteColumn.ImageLayout =  DataGridViewImageCellLayout.Stretch;
            dataGridView3.Columns.Add(deleteColumn2);

            DataGridViewTextBoxColumn cx_name = new DataGridViewTextBoxColumn();
            cx_name.HeaderText = "承运单位";
            cx_name.DataPropertyName = "cx_name";
            dataGridView3.Columns.Add(cx_name);


            DataGridViewTextBoxColumn cx_ss = new DataGridViewTextBoxColumn();
            cx_ss.HeaderText = "审核类型";
            cx_ss.DataPropertyName = "cx_ss";
            dataGridView3.Columns.Add(cx_ss);

            DataGridViewTextBoxColumn cx_sh = new DataGridViewTextBoxColumn();
            cx_sh.HeaderText = "待审核车型数量";
            cx_sh.DataPropertyName = "cx_sh";
            dataGridView3.Columns.Add(cx_sh);

            DataGridViewTextBoxColumn cx_id = new DataGridViewTextBoxColumn();
            cx_id.Visible = false;
            cx_id.HeaderText = "待审核车型数量";
            cx_id.DataPropertyName = "cx_id";
            dataGridView3.Columns.Add(cx_id);






            DataGridViewTextBoxColumn jw_bian = new DataGridViewTextBoxColumn();
            jw_bian.HeaderText = "阶梯";
            jw_bian.DataPropertyName = "jw_bian";
            dataGridView1.Columns.Add(jw_bian);


            DataGridViewTextBoxColumn jw_jt = new DataGridViewTextBoxColumn();
            jw_jt.HeaderText = "阶梯位";
            jw_jt.DataPropertyName = "jw_jt";
            dataGridView1.Columns.Add(jw_jt);

            DataGridViewTextBoxColumn jw_jg = new DataGridViewTextBoxColumn();
            jw_jg.HeaderText = "价格";
            jw_jg.DataPropertyName = "jw_jg";
            dataGridView1.Columns.Add(jw_jg);
        }

        
    }
}
